package com.zhanggm.springcloud.order.controller;

import com.zhanggm.springcloud.order.pojo.Order;
import com.zhanggm.springcloud.order.service.OrderService;
import com.zhanggm.springcloud.user.api.UserApi;
import com.zhanggm.springcloud.user.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/order/")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private UserApi userApi;

    /**
     * 查询订单
     * @param id
     * @return
     */
    @RequestMapping("getById")
    public Order getOrderById(@RequestParam("id") Integer id){
        Order order = orderService.getById(id);
        //调用user服务，返回user对象
//        User user = restTemplate.getForObject("http://service-user/user/getUserById?id="+order.getUserId(), User.class);
        User user = userApi.getUserById(order.getUserId());
        order.setUsername(user.getUsername());
        return order;
    }

}
